﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace XamlaApp
{
    /// <summary>
    /// Helper class providing commonly used extension methods
    /// </summary>
    public static class XamlaExtensions
    {
        /// <summary>
        /// Determines if a given Type implements a given interface Type
        /// </summary>
        /// <param name="obj">The Type to test agains the interface Type</param>
        /// <param name="InterfaceType">The interface Type</param>
        /// <returns></returns>
        public static bool ImplementsInterface(this Type obj, Type InterfaceType)
        {
            return InterfaceType.IsAssignableFrom(obj);
        }

        /// <summary>
        /// Centers a given element to a given Canvas element.  
        /// Note that this does NOT place the item into the canvas tree, only positions it relative to the canvas dimensions.
        /// </summary>
        /// <param name="elementToCenter">The element to center to the given canvas.</param>
        /// <param name="canvasToCenterTo">The canvas that the element should be centered to.</param>
        public static void CenterElementToCanvas(this FrameworkElement elementToCenter, Canvas canvasToCenterTo)
        {
            double cw, ch, ew, eh;

            if (!double.IsNaN(canvasToCenterTo.Width))
            {
                cw = canvasToCenterTo.Width;
                ch = canvasToCenterTo.Height;
            }
            else if (!double.IsNaN(canvasToCenterTo.ActualWidth))
            {
                cw = canvasToCenterTo.ActualWidth;
                ch = canvasToCenterTo.ActualHeight;
            }
            else return;

            if (!double.IsNaN(elementToCenter.Width))
            {
                ew = elementToCenter.Width;
                eh = elementToCenter.Height;
            }
            else if (!double.IsNaN(elementToCenter.ActualWidth))
            {
                ew = elementToCenter.ActualWidth;
                eh = elementToCenter.ActualHeight;
            }
            else return;

            Canvas.SetTop(elementToCenter, (ch / 2) - (eh / 2));
            Canvas.SetLeft(elementToCenter, (cw / 2) - (ew / 2));
        }
    }
}
